package net.mingsoft.report_data.action.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import net.mingsoft.base.entity.ResultData;
import net.mingsoft.cms.dao.IContentDao;
import net.mingsoft.cms.entity.ContentEntity;
import net.mingsoft.report_data.action.q.HtmlQuery;
import net.mingsoft.report_data.action.r.ReportTypeResponse;
import net.mingsoft.report_data.biz.ZkDataBiz;
import net.mingsoft.report_data.dao.ZkDataDao;
import net.mingsoft.report_data.enums.PeriodEnum;
import net.mingsoft.report_data.enums.ReportType;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @author gengfa
 * @date 22/03//18
 */
@Api(value = "报告数据接口", tags = {"根据指标自动生成报告"})
@RestController
@RequestMapping("/${ms.manager.path}/report")
public class ReportDataAction {

    @Resource
    ZkDataBiz dataBiz;

    @Resource
    IContentDao contentDao;

    @ApiOperation(value = "生成报告：传指标ID")
    @PostMapping(value = "html", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses(value = {
            @ApiResponse(code = 0, message = "success", response = String.class)
    })
    public ResultData generateHtml(@RequestBody HtmlQuery query) {
        if (CollectionUtils.isEmpty(query.getIds())) {
            return ResultData.build().error("illegal param");
        }
        return ResultData.build().success(dataBiz.genReport(query));
    }

    @ApiOperation(value = "获取报告类型")
    @GetMapping(value = "type")
    @ApiResponses(value = {
            @ApiResponse(code = 0, message = "success", response = ReportTypeResponse.class)
    })
    public ResultData getReportType() {
        return ResultData.build().success(dataBiz.getReportTypes());
    }

    @ApiOperation(value = "获取报告指标")
    @GetMapping(value = "metric")
    @ApiResponses(value = {
            @ApiResponse(code = 0, message = "success", response = ReportTypeResponse.class)
    })
    public ResultData getReportMetrics(@RequestParam(value = "type") String type) {
        return ResultData.build().success(dataBiz.getReportMetric(type));
    }

    @ApiOperation(value = "预览HTML（传报告编号）")
    @GetMapping("preview/html")
    void previewHtml(@RequestParam("id") String id, HttpServletResponse response) throws IOException {
        //String html = dataBiz.genReport(PeriodEnum.MONTH, ReportType.POPULATION_ANALYSIS);
        ContentEntity contentEntity = contentDao.selectById(id);
        String html = contentEntity.getContentDetails();
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(html.getBytes());
    }

    @ApiOperation(value = "导出PDF（传报告编号）")
    @GetMapping("export/pdf")
    void exportPdf(@RequestParam("id") String id, HttpServletResponse response) throws IOException {
        //String html = dataBiz.genReport(PeriodEnum.MONTH, ReportType.POPULATION_ANALYSIS);
        ContentEntity contentEntity = contentDao.selectById(id);
        String html = contentEntity.getContentDetails();
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition", "attachment;fileName=" +
                new String("example.pdf".getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(html.getBytes());
    }
}
